import os
import sys

import time

import pyautogui

from driver.driver import mouse_move_to, get_control_by_level, get_control_position, click, get_current_position_rgb, \
    mouse_move_to_and_click, get_position_rgb, center_position, change_cwd

gongji = (833, 205)
gongji1 = (833, 233)
cheng_arr = [
    (573, 360),
    (725, 395),
    (810, 385), (865, 420),
    (740, 470), (805, 475),
    (590, 275),
    (515, 285), (440, 275),
    (550, 200), (540, 135),
    (730, 290), (800, 220), (860, 300), (935, 260), (910, 210), (830, 150), (900, 135)]
# jundui = (690, 530)
chuang = (353, 198)
tou = (410, 200)
junzhong_arr = [chuang, tou]
celue = (50, 66)


# @change_cwd(os.getcwd(), os.path.dirname(os.path.abspath(__file__)))
def entry_func():
    # time.sleep(10)
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)
    gongji_button = (gongji[0] + left, gongji[1] + top)
    gongji_button1 = (gongji1[0] + left, gongji1[1] + top)
    jundui_button = None
    # jundui_button = (jundui[0] + left, jundui[1] + top)
    end_flag = False
    for cheng in cheng_arr:
        i = 0
        while True:
            cheng_left = left + cheng[0]
            cheng_top = top + cheng[1]
            mouse_move_to_and_click(cheng_left, cheng_top)
            time.sleep(1)
            if get_position_rgb(gongji_button1[0], gongji_button1[1]) == (159, 55, 50):
                mouse_move_to_and_click(gongji_button1[0], gongji_button1[1])
            elif get_position_rgb(gongji_button[0], gongji_button[1]) == (158, 54, 49):
                mouse_move_to_and_click(gongji_button[0], gongji_button[1])
                if cheng == cheng_arr[-1]:
                    end_flag = True
            elif get_position_rgb(gongji_button[0], gongji_button[1]) != (158, 54, 49):
                mouse_move_to_and_click(left + 870, top + 470)
                break
            mouse_move_to(left + 830, top + 500)
            if i == 0:
                i = i + 1
                while pyautogui.locateOnScreen("./imgs/Battle.jpg", confidence=0.9) is None:
                    time.sleep(5)
                jundui_button = center_position("./imgs/Battle.jpg", confidence=0.9)
            time.sleep(2)
            # 开始战斗了,点击军队
            mouse_move_to(jundui_button[0], jundui_button[1])
            click()
            # 点击结束战斗
            mouse_move_to_and_click(left + 530, top + 515)
            # 点击战术2
            mouse_move_to_and_click(left + 360, top + 530)
            mouse_move_to_and_click(left + 530, top + 515)
            for i in range(8):
                click()
                time.sleep(0.1)
            # 点击关闭
            mouse_move_to(left + 930, top + 515)
            if end_flag:
                mouse_move_to(left + 830, top + 510)
                os._exit(0)
            click()
        # 配置军队
        mouse_move_to(left + 680, top + 540)
        click()
        mouse_move_to(left + 680, top + 560)
        click()
        mouse_move_to(left + 770, top + 125)
        click()
        mouse_move_to(left + 770, top + 175)
        click()
        mouse_move_to(left + 600, top + 500)
        click()


def test1():
    control = get_control_by_level([0, 0, 0, 0, 0, 0], base_name="热血三国-")
    left, top, width, height = get_control_position(control)
    gongji_button = (gongji[0] + left, gongji[1] + top)
    gongji_button1 = (gongji1[0] + left, gongji1[1] + top)
    print(get_position_rgb(gongji_button1[0], gongji_button1[1]) == (159, 55, 50))
    print(get_position_rgb(gongji_button[0], gongji_button[1]) == (158, 54, 49))
    print(get_position_rgb(gongji_button1[0], gongji_button1[1]))


if __name__ == "__main__":
    entry_func()
    # print(pyautogui.locateOnScreen("./imgs/Battle.jpg", confidence=0.9))
    # test1()
